package com.tianna.city;

import java.util.List;
import java.util.Random;

import com.tianna.swipelistview.SwipeListView;
import com.tianna.db.*;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SwipeAdapter extends ArrayAdapter<MyCity> {
	private LayoutInflater mInflater;
	private List<MyCity> objects;
	private SwipeListView mSwipeListView;
	private RelativeLayout re;
	
	private Weather weather;

	public SwipeAdapter(Context context, int textViewResourceId,
			List<MyCity> testData, SwipeListView mSwipeListView) {
		super(context, textViewResourceId, testData);
		this.objects = testData;
		this.mSwipeListView = mSwipeListView;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		weather = new Weather(context);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = mInflater
					.inflate(R.layout.package_row, parent, false);
			holder = new ViewHolder();
			re = (RelativeLayout) convertView.findViewById(R.id.front);
			holder.mFrontText = (TextView) convertView
					.findViewById(R.id.example_row_tv_title); //

			holder.mtemText = (TextView) convertView
					.findViewById(R.id.example_row_tv_temperture); //
			holder.mtypeText = (TextView) convertView
					.findViewById(R.id.example_row_tv_type); //
			holder.mBackDelete = (Button) convertView
					.findViewById(R.id.example_row_b_action_2);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		MyCity item = getItem(position);
		
		int number = new Random().nextInt(9) + 1;
		int resId1 =this.getContext().getResources().getIdentifier("c"+number,"drawable","com.tianna.db");
		
		re.setBackgroundResource(resId1);
		holder.mFrontText.setText(item.getName());
		holder.mtemText.setText(item.getTem());
		holder.mtypeText.setText(item.getWeather());

		

		holder.mBackDelete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
			
				mSwipeListView.closeAnimate(position);
				mSwipeListView.dismiss(position);
			}
		});
		
		
		
		
		return convertView;
	}

	

	class ViewHolder {

		TextView mFrontText;
		TextView mtemText;
		TextView mtypeText;
		Button mBackEdit, mBackDelete;
	}
}
